<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.model.*" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<%  
    SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION);
    String requestObject = (String) st.getValue(FormFields.TOKEN);
    UserContainer parentContainer = (UserContainer) st.removeValue("parentContainer"); 
    Item availableChildren[][] = (Item[][]) st.removeValue("availableChildList");
    Item assignedChildren[][] = (Item[][]) st.removeValue("assignedChildList");
    String editStatus = (String) st.removeValue(FormFields.EDITSTATUS);
    int editorPage = (new Integer((String) st.removeValue(FormFields.EDITORPAGE))).intValue();
    String displayMessage = (String) st.getValue("displayMessage"); 
    st.putValue("displayMessage", "");
    String loginType = (String) request.getAttribute(FormFields.USRTYPE); 
    String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);
%> 


<HTML>
<HEAD>

  <title>Parent Editor Form</title>

    <SCRIPT LANGUAGE="JavaScript">

        function validateRequiredFields() {
            if (document.forms[0].<%= SchoolDBSQL.USRSCHOOLID %>.value == "") {
                alert("You must enter an ID for the parent.");
                document.forms[0].<%= SchoolDBSQL.USRSCHOOLID %>.focus();
                return false;
            }
            else {
                return true;
            }
        }
        
        function validateNewPasswords() {
        
            var newPassword1 = document.forms[0].<%= SchoolDBSQL.USRPASSWORD %>.value;
            var newPassword2 = document.forms[0].<%= SchoolDBSQL.USRPASSWORD %>2.value;
            var userID = document.forms[0].<%= SchoolDBSQL.USRASSIGNEDID %>.value;
            
            if (newPassword1 == "") {
                if (userID == -1) {
                    alert("You must enter a password.");
                    document.forms[0].<%= SchoolDBSQL.USRPASSWORD %>.focus();
                    return false;
                }
                else if (confirm("You did not specify a password. Leave existing password unchanged?")) {
                    return true;
                }
                else {
                    document.forms[0].<%= SchoolDBSQL.USRPASSWORD %>.focus();
                    return false;
                }
            }
            else if (newPassword1 != newPassword2) {
                alert("The new passwords you entered do not match.\nPlease re-enter your new password in both fields.");
                document.forms[0].<%= SchoolDBSQL.USRPASSWORD %>.focus();
                return false;
            }
            else {
                return true;
            }
            
        }
        
        function postProcessing(x) {
            if (validateRequiredFields() && validateNewPasswords()) {
                document.forms[0].<%= FormFields.SAVEPARENT %>.value = x;
                buildAssignedChildString();
                document.forms[0].submit();
            }
        }
    
        var availableArray = new Array();
        var assignedArray = new Array();
        var d = top.frames.viewframe.document;
        var padding = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
        
        function init() { 
            <% 
            for (int i = 0; i < availableChildren.length; i++) {
                out.println("insertOptionIntoArray(availableArray, \"" + availableChildren[i][0].toString() + "\", \"" + availableChildren[i][1].toString()+"\", \"" + availableChildren[i][2].toString() + "\");");
                out.println("insertOptionIntoList(d.forms[0].availableList, \"" + availableChildren[i][0] + "\", \"" + availableChildren[i][2] + ", " + availableChildren[i][1] + "\");");
            }
            for (int i = 0; assignedChildren != null && i < assignedChildren.length; i++) {
                out.println("insertOptionIntoArray(assignedArray, \"" + assignedChildren[i][0].toString()+"\", \"" + assignedChildren[i][1].toString() + "\", \"" + assignedChildren[i][2].toString() + "\");");
                out.println("insertOptionIntoList(d.forms[0].assignedList, \"" + assignedChildren[i][0] + "\", \"" + assignedChildren[i][2] + ", " + assignedChildren[i][1] + "\");");
                out.println("removeOptionFromArray(availableArray, \""+assignedChildren[i][0].toString()+"\");");
                out.println("removeOptionFromList(d.forms[0].availableList, \""+assignedChildren[i][0].toString()+"\");");
            }
            %>
            if ("<%= displayMessage %>" != "null" && "<%= displayMessage %>" != "") {
              top.frames.mainframe.notify("<%= displayMessage %>");
            }
        }

        function viewDetailedInfo(page, selectedItem) {
            top.frames.mainframe.document.forms[0].<%= FormFields.SUBLINK %>.value = selectedItem;
            top.frames.mainframe.submitPage(top.frames.mainframe.document, "<%= FormFields.EDITORPAGE %>", page);
            return false;
        }

        function existsInArray(anArray, key) {
            for (var i in anArray) {
                if (anArray[i][0] == key) {
                    return true;
                }
            }
            
            return false;
        }
        
        function existsInList(aList, key) {
            for (var i = 0; i < aList.length; i++) {
                if (aList[i].value == key) {
                    return true;
                }
            }
            
            return false;
        }

        function insertOptionIntoArray(anArray, avalue, bvalue, cvalue) {
            if (!existsInArray(anArray, avalue)) {
                anArray[anArray.length] = new Array(3);
//                var newArray = new Array();
//                newArray[0] = avalue;
//                newArray[1] = bvalue;
//                newArray[2] = cvalue;
                anArray[anArray.length-1][0] = avalue;
                anArray[anArray.length-1][1] = bvalue;
                anArray[anArray.length-1][2] = cvalue;
    
//                anArray.push(newArray);
            }
        }
        
        function insertOptionIntoList(selectList, value, text) {
            if (!existsInList(selectList, value)) {
                var o = new Option(text, value);
                var temp;
                for (var i = 0; i < selectList.length; i++) {
                    if (selectList[i].text > o.text) {
                        temp = selectList[i];
                        selectList[i] = o;
                        o = temp;
                    }
                }
                selectList[selectList.length]=o;
            }
        }
        
        function removeOptionFromArray(anArray, key) {
//            var tempArray;
//            var returnVal = null;
//            for (var i = 0; i < anArray.length; i++) {
//                if (anArray[i][0] == key) {
//                    returnVal = anArray.splice(i, 1)[0];
//                    return returnVal;
//                }
//            }
			var returnVal = null;
			for (var i = 0; i < anArray.length; i++) {
				if (anArray[i][0] == key) {
					returnVal = anArray[i];
					var j = i + 1;
					for (; j < anArray.length; j++, i++) {
						anArray[i] = anArray[j];
					}
					anArray[j] = null;
					anArray.length = j;
			
					return returnVal;
				}
			}
        }
        
        function removeOptionFromList(selectList, key) {
            var returnVal = null;
            for (var i = 0; i < selectList.length; i++) {
                if (selectList[i].value == key) {
                    returnVal = selectList[i];
                    selectList[i] = null;
                    return returnVal;
                }
            }
        }

        function selectAvailable() {
            var selectedValues = new Array();
            var w = top.frames.viewframe;
            var d = w.document;
            var availList = d.forms[0].availableList;
            var assgnList = d.forms[0].assignedList;
            var counter = 0;

            for (var i = 0; i < availList.length; i++) {
                var arrayTemp = null;
                var listTemp = null;
                if (availList[i].selected) {
                    arrayTemp = removeOptionFromArray(availableArray, availList[i].value);
                    insertOptionIntoArray(assignedArray, arrayTemp[0], arrayTemp[1], arrayTemp[2]);
                    listTemp = removeOptionFromList(availList, availList[i].value);
                    insertOptionIntoList(assgnList, listTemp.value, listTemp.text);
                    i--;
                }
            }
        }
        
        function unselectAssigned() {
            var selectedValues = new Array();
            var w = top.frames.viewframe;
            var d = w.document;
            var availList = d.forms[0].availableList;
            var assgnList = d.forms[0].assignedList;
            var counter = 0;

            for (var i = 0; i < assgnList.length; i++) {
                var arrayTemp = null;
                var listTemp = null;
                if (assgnList[i].selected) {
                    arrayTemp = removeOptionFromArray(assignedArray, assgnList[i].value);
                    insertOptionIntoArray(availableArray, arrayTemp[0], arrayTemp[1], arrayTemp[2]);
                    listTemp = removeOptionFromList(assgnList, assgnList[i].value);
                    insertOptionIntoList(availList, listTemp.value, listTemp.text);
                    i--;
                }
            }
        }
        
        function buildAssignedChildString() {
            var result = "";
            for (var i=0; i < assignedArray.length; i++) {
                if (i == 0) {
                    result = assignedArray[i][0];
                }
                else {
                    result = result + "," + assignedArray[i][0];
                }
            }
            document.forms["parenteditorform"].ASSIGNEDCHILDREN.value = result;
            return true;
        }

        function cancelForm() {
            document.forms[0].<%= FormFields.LINKCLICKED %>.value = "<%= FormFields.CANCEL %>";
            document.forms[0].submit();
        }

    </SCRIPT>
  </HEAD>
  <BODY <% if (!editStatus.equals(FormFields.VIEW)) {
               out.print("onLoad=\"init()\"");
           }
           else if (parentContainer != null) {
               out.print("onLoad=\"top.frames.mainframe.highlightByID('" + parentContainer.getAssignedID() + "');\"");
           }
        %>>

    <FORM NAME="parenteditorform" METHOD="POST" onSubmit="return false;">
        <INPUT TYPE="HIDDEN" NAME="ASSIGNEDCHILDREN" VALUE="">
        <INPUT TYPE="HIDDEN" NAME="<%= SchoolDBSQL.USRASSIGNEDID %>" VALUE="<%= parentContainer.getAssignedID().toString() %>">
        <INPUT TYPE="HIDDEN" NAME="<%= SchoolDBSQL.USRTYPE %>" VALUE="<%= SchoolDBSQL.PARENT %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= requestObject%>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="<%= FormFields.EDPARENTPAGE %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="<%= FormFields.SAVEACTION %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.EDITORPAGE %>" VALUE="<%= editorPage %>"> 
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.SAVEPARENT %>" VALUE=""> 
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">

            <TABLE  style="font-size:12;font-family:sans-serif;">
            <TR>
            <TD colspan="2">
                <H2>Parent Editor</H2><br>
            </TD>
            </TR>
            <TR>
                <TH ALIGN="LEFT">Parent ID</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("                <INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.USRSCHOOLID + "\" VALUE=\"" + parentContainer.getSchoolID().toString() + "\">");
                        }
                        else {
                            out.println(parentContainer.getSchoolID().toString());
                        }
                    %>
                </TD>
            </TR>

            <TR>
                <TH ALIGN="LEFT">Title</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("                <INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.USRTITLE + "\" VALUE=\"" + parentContainer.getTitle().toString() + "\">");
                        }
                        else {
                            out.println(parentContainer.getTitle().toString());
                        }
                    %>
                </TD>
            </TR>

            <TR>
                <TH ALIGN="LEFT">Parent's First Name</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("                <INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.USRFNAME + "\" VALUE=\"" + parentContainer.getFirstName().toString() + "\">");
                        }
                        else {
                            out.println(parentContainer.getFirstName().toString());
                        }
                    %>
                </TD>
            </TR>

            <TR>
                <TH ALIGN="LEFT">Parent's Last Name</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("                <INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.USRLNAME + "\" VALUE=\"" + parentContainer.getLastName().toString() + "\">");
                        }
                        else {
                            out.println(parentContainer.getLastName().toString());
                        }
                    %>
                </TD>
            </TR>

                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("            <TR>");
                            out.println("                <TH ALIGN=\"LEFT\">Parent's Password</TH>");
                            out.println("                <TD>");
                            out.println("                <INPUT TYPE=\"PASSWORD\" NAME=\"" + SchoolDBSQL.USRPASSWORD + "\" VALUE=\"\">");
                            out.println("                </TD>");
                            out.println("                <TH ALIGN=\"LEFT\">Re-enter Password</TH>");
                            out.println("                <TD>");
                            out.println("                <INPUT TYPE=\"PASSWORD\" NAME=\"" + SchoolDBSQL.USRPASSWORD + "2\" VALUE=\"\">");
                            out.println("                </TD>");
                            out.println("            </TR>");
                        }
                    %>

            <TR>
                <TH ALIGN="LEFT">Parent's Phone Number</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("                <INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.USRPHONE + "\" VALUE=\"" + parentContainer.getPhone().toString() + "\">");
                        }
                        else {
                            out.println(parentContainer.getPhone().toString());
                        }
                    %>
                </TD>
            </TR>

            <TR>
                <TH ALIGN="LEFT">Parent's Email Address</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("                <INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.USREMAIL + "\" VALUE=\"" + parentContainer.getEmail().toString() + "\">");
                        }
                        else {
                            out.println(parentContainer.getEmail().toString());
                        }
                    %>
                </TD>
            </TR>

                <%
                    if (editStatus.equals(FormFields.VIEW)) {
                        out.println("            <TR>");
                        out.println("                <TH ALIGN=\"LEFT\" VALIGN=\"TOP\">Children</TH>");
                        out.println("                <TD>");
                        for (int i = 0; i < assignedChildren.length; i++) {
                            out.println("<a href=\"\" onClick=\"viewDetailedInfo('" + FormFields.EDSTUDENTPAGE + "','" + assignedChildren[i][0] + "');return false;\">" + assignedChildren[i][2] + ", " + assignedChildren[i][1] + "</a><br>");
                        }
                        out.println("                </TD>");
                        out.println("            </TR>");
                    }
                    else {
                        out.println("            <TR>");
                        out.println("                <TD COLSPAN=\"5\">");
                        out.println("                    <TABLE>");
                        out.println("                        <TR>");
                        out.println("                            <TD ALIGN=\"CENTER\" COLSPAN=\"2\"><H3>Children of this parent</H3></TD>");
                        out.println("                            <TD>&nbsp</TD>");
                        out.println("                            <TD ALIGN=\"CENTER\" COLSPAN=\"2\"><H3>Available children</H3></TD>");
                        out.println("                        </TR>");
                        out.println("                        <TR>");
                        out.println("                            <TD ALIGN=\"CENTER\" COLSPAN=\"2\">");
                        out.println("                              <SELECT NAME=\"assignedList\" MULTIPLE=\"YES\" SIZE=\"4\" STYLE=\"WIDTH:250PX\"");
                        out.println("</TD>");
                        out.println("                            <TD>&nbsp;&nbsp;<INPUT TYPE=\"BUTTON\" VALUE=\"<--\" onClick='selectAvailable()'>&nbsp;&nbsp;<BR>");
                        out.println("                                &nbsp;<BR>");
                        out.println("                                &nbsp;&nbsp;<INPUT TYPE=\"BUTTON\" VALUE=\"-->\" onClick='unselectAssigned()'>&nbsp;&nbsp;</TD>");
                        out.println("                            <TD ALIGN=\"CENTER\" COLSPAN=\"2\">");
                        out.println("                              <SELECT NAME=\"availableList\" MULTIPLE=\"YES\" SIZE=\"4\" STYLE=\"WIDTH:250PX\"");
                        out.println("                            </TD>");
                        out.println("                        </TR>");
                        out.println("                    </TABLE>");
                        out.println("                </TD>");
                        out.println("            </TR>");
                    }
            %>
            <TR>
                <TD COLSPAN=2>&nbsp;</TD>
            </TR>

            <TR>
                <%
                    if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                        out.println("                <TD><INPUT NAME=\"" + FormFields.SAVEPARENT + "\" TYPE=\"BUTTON\" VALUE=\"Save\" onClick=\"postProcessing('Save')\"></TD>");
                        out.println("                <TD><INPUT NAME=\"cancel\" TYPE=\"BUTTON\" VALUE=\"Cancel\" onClick='cancelForm()'></TD>");
                    }
                %>
            </TR>
        </TABLE>

    </FORM>
</HTML>

